package com.serviceprovider.business.module.user.controller;

//~--- non-JDK imports --------------------------------------------------------

import com.serviceprovider.business.module.admin.user.services.dao.interfaces.IUserService;
import com.serviceprovider.business.module.exception.NotificationService;
import com.serviceprovider.business.module.user.model.impl.User;
import com.serviceprovider.business.utility.RequestConstants;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

//~--- JDK imports ------------------------------------------------------------

import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

@Controller
public class ForgotPasswordController {
    String mailRegEx =
        "(^$|^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\\-+)|([A-Za-z0-9]+\\.+)|([A-Za-z0-9]+\\++))*[A-Za-z0-9]+@((\\w+\\-+)|(\\w+\\.))*\\w{1,63}\\.[a-zA-Z]{2,6}$)";
    @Autowired
    private NotificationService emailNotificationService;
    @Autowired
    private MailSender          mailSender;
    @Autowired
    private IUserService        userService;

    @RequestMapping(
        value  = RequestConstants.ForgottenPasswordRequests.FORGOTTEN_PASSWORD,
        method = RequestMethod.GET
    )
    public ModelAndView showForgottenPasswordForm() {
        ModelAndView mav = new ModelAndView(RequestConstants.ForgottenPasswordRequests.VIEW_FORGOTTEN_PASSWORD);

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.ForgottenPasswordRequests.FORGOTTEN_PASSWORD,
        method = RequestMethod.POST
    )
    public ModelAndView renewForgottenPassword(HttpServletRequest request) {
        String       viewName = RequestConstants.ForgottenPasswordRequests.VIEW_FORGOTTEN_PASSWORD;
        String       email    = request.getParameter("email");
        Pattern      pattern  = Pattern.compile(mailRegEx);
        ModelAndView mav      = new ModelAndView();
        boolean      errors   = false;

        if ((email == null) || (email.trim().length() < 1)) {
            mav.getModel().put("emailError", "forgotten_password.validation.missing_email");
            errors = true;
        } else if (!pattern.matches(mailRegEx, email)) {
            errors = true;
            mav.getModel().put("emailError", "forgotten_password.validation.invalid_mail");
        } else if (userService.getByEmail(email.trim()) == null) {
            errors = true;
            mav.getModel().put("emailError", "forgotten_password.validation.not_existing_email");
        }

        if (!errors) {
            User user = userService.getByEmail(email.trim());

            emailNotificationService.sendNotification(email,
                    "Following is the new password to login to Service Provider System: \"" + user.getPassword()
                    + "\"", "New Service Provider Password");
            mav = new ModelAndView("redirect:" + RequestConstants.UserRequests.LOGIN);
        } else {
            mav.setViewName(viewName);
        }

        return mav;
    }
}



